package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.VillageMapper;
import com.kgc.community.pojo.Village;
import com.kgc.community.pojo.VillageExample;
import com.kgc.community.service.VillageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@Service
public class VillageServiceImpl implements VillageService {

    @Resource
    VillageMapper villageMapper;


    @Override
    public PageInfo<Village> selectAll(Date buildTime,Date buildTime2, String describes, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id");
        VillageExample villageExample=new VillageExample();
        if(buildTime!=null){
            villageExample.createCriteria().andBuildTimeEqualTo(buildTime);
        }
        if(buildTime2!=null){
            villageExample.createCriteria().andBuildTimeEqualTo(buildTime2);
        }
        if (describes!=null){
            villageExample.createCriteria().andDescribesLike('%'+describes+'%');
        }
        List<Village> villages = villageMapper.selectByExample(villageExample);
        PageInfo<Village> pageInfo=new PageInfo<>(villages);
        return pageInfo;
    }


    @Override
    public int Count(Village village) {
        VillageExample villageExample=new VillageExample();
        List<Village> villages = villageMapper.selectByExample(villageExample);
        return villages.size();
    }

    @Override
    public Integer add(Village village) {

        return villageMapper.insertSelective(village);
    }

    @Override
    public List<Village> selectByExample() {
        List<Village> villages = villageMapper.selectByExample(null);
        return villages;
    }

    @Override
    public Village selectId(Integer id) {
      return villageMapper.selectByPrimaryKey(id);
    }


    @Override
    public int update(Village village) {

        return villageMapper.updateByPrimaryKeySelective(village);
    }

    @Override
    public int deleteVillageById(Integer id) {
        return villageMapper.deleteByPrimaryKey(id);
    }


}
